<%@page contentType="text/html" pageEncoding="utf-8"%>
<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<link href="../css/main.css" type="text/css" rel="stylesheet" />
		<link href="../css/dialog.css" rel="stylesheet" type="text/css">
		<title>企业书屋运营管理平台</title>
	</head>
	<body>
		<div class="main_frame">
			<div class="position_div">
				<img src="../images/position_icon.png" />
				<i>系统管理</i><i>&gt;&gt;</i><i>用户管理</i>
			</div>
			<div class="right_div">
				<div class="search_div">
					<div class="label_div">
						<label class="text_label">
							帐号：
						</label>
						<input type="text" class="input_search" placeholder="支持模糊查询" id="qry_account" />
					</div>
					<div class="label_div">
						<label class="text_label">
							姓名：
						</label>
						<input type="text" class="input_search" placeholder="支持模糊查询" id="qry_name" />
					</div>
					<div class="done_div">
						<button class="btn_common1 title_div2"  onclick="qryUserListByCondition()">
							<i></i>搜索
						</button>
						<button style="margin-left:10px !important" class="btn_common2 newadd"  onclick="addUser()">
							<i></i>新增
						</button>
					</div>
                    <div class="cl">&nbsp;</div>
				</div>
				<table class="table_list" id="tab1">
					<tr>
						<th width="18%">
							帐号
						</th>
						<th width="17%">
							姓名
						</th>
						<th width="15%">
							角色
						</th>
						<th width="10%">
							状态
						</th>
						<!-- <th width="10%">
							
						</th> -->
						<th width="20%">
							创建时间
						</th>
						<th class="last_td">
							基本操作
						</th>
					</tr>
					<tbody id="listbox1">
					</tbody>
				</table>
				
				<!-- 添加用户 -->
			   <div class="right_content_edit" id="addUserBox">
				    <div class="blank_div"></div>
					<label class="text_label label_width">
						用户角色：
					</label>
					<div class="drop_frame">
						<i class="text_i" data-status="0" id="add_roleID">选择角色</i><span
							class="span_arrow"></span>
						<ul class="choose_ul" id="prole_choose_add">
						</ul>
					</div>
					&nbsp;&nbsp;<span style="color: red">*</span>
				    <div class="blank_div"></div>
				    <label class="text_label label_width">用户帐号：</label>
				    <input type="text" class="input_search width_input" id="add_account"/>
				    &nbsp;&nbsp;<span style="color: red">*</span>
				    <div class="blank_div"></div>
				    <label class="text_label label_width">用户姓名：</label>
				    <input type="text" class="input_search width_input" id="add_name"/>
				    &nbsp;&nbsp;<span style="color: red">*</span>
				    <div class="blank_div"></div>
				    <label class="text_label label_width">帐号密码：</label>
				    <input type="password" class="input_search width_input" id="add_pwd"/>
				    &nbsp;&nbsp;<span style="color: red">*</span>
				    <div class="blank_div"></div>
				    <div class="blank_div"></div>
				   <!--  <label class="text_label label_width">企业编号：</label>
				    <input type="text" class="input_search width_input" id="add_EnterpriseId"/>
				    &nbsp;&nbsp;<span style="color: red">*</span> -->
			    	<div class="bottom_donebtn width_div">
			    		<button class="btn_common2" id="addUserBtn" onclick="doAddUser()">提交</button>
			    		<button class="btn_common1" onclick="edit_cancel(1)">取消</button>
			    	</div>
			    	<div class="blank_div"></div>
				    <div class="blank_div"></div>
			  	</div>
			  	<!-- 修改用户 -->
				<div class="right_content_edit" id="editUserBox">
				<input type="hidden" name="edit_userID" id="edit_userID" />
					<div class="blank_div"></div>
					<label class="text_label label_width">
						用户角色：
					</label>
					<div class="drop_frame">
						<i class="text_i" data-status="0" id="edit_roleID">选择角色</i><span
							class="span_arrow"></span>
						<ul class="choose_ul" id="prole_choose_edit">
						</ul>
					</div>
					&nbsp;&nbsp;<span style="color: red">*</span>
					<div class="blank_div"></div>
					<label class="text_label label_width">
						用户状态：
					</label>
					<div class="drop_frame">
						<i class="text_i" data-status="" id="edit_status">选择状态</i><span
							class="span_arrow"></span>
						<ul class="choose_ul">
							<li>
								<a href="javascript:void(0)" class="choose_a" data-status="1">正常</a>
							</li>
							<li>
								<a href="javascript:void(0)" class="choose_a" data-status="0">禁用</a>
							</li>
						</ul>
					</div>
					&nbsp;&nbsp;<span style="color: red">*</span>
					<div class="blank_div"></div>
					<label class="text_label label_width">
						用户帐号：
					</label>
					<input type="text" class="input_search width_input" id="edit_account"  disabled="disabled" />
					&nbsp;&nbsp;<span style="color: red">*</span>
					<div class="blank_div"></div>
					<label class="text_label label_width">
						用户姓名：
					</label>
					<input type="text" class="input_search width_input" id="edit_name" />
					&nbsp;&nbsp;<span style="color: red">*</span>
					<div class="blank_div"></div>
				   <!--  <label class="text_label label_width">企业编号：</label>
				    <input type="text" class="input_search width_input" id="edit_EnterpriseId"/>
				    &nbsp;&nbsp;<span style="color: red">*</span> -->
				    <div class="blank_div"></div>
			    	<div class="bottom_donebtn width_div">
			    	<input type="hidden" id="editUid" value=""/>
			    		<button class="btn_common2" id="editMenuBtn" onclick="doEditUser()">提交</button>
			    		<button class="btn_common1" onclick="edit_cancel(2)">取消</button>
			    	</div>
			    	<div class="blank_div"></div>
			    	<div class="blank_div"></div>
			  	</div>
				<!--分页 -->
				<div class="turn_page" style="display:none">
				   <div class="main_page" id="pageCon">
					   <a href="javascript:void(0)" class="first_a" id="firstPage"></a>
					   <a href="javascript:void(0)" class="up_a" id="prev"></a>    	
					   <a href="javascript:void(0)" class="next_a" id="next"></a>
					   <a href="javascript:void(0)" class="last_a" id="lastPage"></a>
					   <input id="page_num_input" style="width:40px;" type="text" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"/>
					   <button type="button" class="num_input" id="go_num_input">跳转</button>
					   <b class="page_text">当前<i><em id="nowpage"></em>/<em id="totalpage"></em></i>页，总数<i id="totalNum"></i>条</b>    	
				   </div>
				</div>
			</div>
		</div>
		<script type="text/javascript" src="../js/jquery-1.10.2.min.js"></script>
		<script type="text/javascript" src="../js/common.js"></script>
		<script type="text/javascript" src="../js/dialog.js"></script>
		<script type="text/javascript" src="../js/jquery.md5.js"></script>
		<script>
		var yema=1;
		var pagelen=10;
		var totalPage=0;
		$(document).ready(function() {
			qryUserList();
			
		});
		//查询按钮触发事件
		function qryUserListByCondition() {
			yema=1;
			qryUserList();
		}
		//获取列表
		function qryUserList() {
			var account = $("#qry_account").val();
			var name = $("#qry_name").val();
			$.ajax({
				url: 'getUserList.do', 
				type: 'POST',
				data:{account:account,name:name,yema:yema,pagelen:pagelen}, 
				async: false,
				dataType: 'json', 
				timeout: 10000, 
				error: function(){LQWarn('请求失败,请稍后重试');}, 
				success: function(result){
					if(result.resultCode==1000){
						var items=result.returnObject.users;
						if (items.length > 0) {
							var pageMsg=result.returnObject.pageMsg;
							var rlist="";
							$.each(items,function(i){
								var item = items[i];
								var status=item.status;
								if(status==1){
									status="正常";
								}else if(status==2){
									status="删除";
								}else if(status==0){
									status="禁用";
								}
								rlist+='<tr data-uid='+item.id+'><td>'+item.account+'</td><td>'+item.name+'</td><td>'+item.roleName+'</td><td data-status="'+item.status+'">'+status+'</td>';
							/* 	rlist+='<td>'+item.organizationId+'</td>'; */ 
							       rlist+='<td>'+formatToDateTime(item.createTime)+'</td><td class="last_td"><input type="hidden" value='+item.U_ID+' name="userID"/><input type="hidden" value='+item.roleId+' name="roleID"/><input type="hidden" value='+item.STATUS+' name="status"/><p class="mod" onclick="editUser(this)">修改</p><p class="del" onclick="delUser('+item.id+')">删除</p></td></tr>';
								/* rlist+='<td>'+formatToDateTime(item.createTime)+'</td><td class="last_td"><input type="hidden" value='+item.U_ID+' name="userID"/><input type="hidden" value='+item.roleId+' name="roleID"/><input type="hidden" value='+item.STATUS+' name="status"/><button class="edit_btn btn_motify" onclick="editUser(this)" title="修改">修改</button><button class="edit_btn btn_delete" onclick="delUser('+item.id+')" title="删除">删除</button></td></tr>'; */
							});
							$("#listbox1").html(rlist);
							totalPage=pageMsg.totalPage;
							$("#nowpage").html(pageMsg.page);
							$("#totalpage").html(pageMsg.totalPage);
							$("#totalNum").html(pageMsg.totalNum);
							$(".turn_page").show();
						}else{
							$("#listbox1").html("");
							$(".turn_page").hide();
						}
					}
					else if(result.resultCode==1004){
						LQWarn(result.returnObject);
						window.top.location.href ="../login.jsp";
					}else{
						LQWarn(result.returnObject);
					}
				}
			});
		}
		$("#firstPage").on("click",function(){
			yema=1;
			qryUserList();		
		});
		$("#lastPage").on("click",function(){
			yema=totalPage;
			qryUserList();
		});
		$("#prev").on("click",function(){
			if(yema>1){
				yema=yema-1;
				qryUserList();
			}
		});
		$("#next").on("click",function(){
			if(yema<totalPage){
				yema=yema+1;
				qryUserList();
			}
		});
		$("#go_num_input").on("click",function(){
			var page_num = $("#page_num_input").val();
			if(page_num == ""){
				LQWarn("请先输入页码");
				return;
			}
			if(page_num-1<totalPage){
				yema=parseInt(page_num);
				qryUserList();
			}else{
				LQWarn("不能超出总页数");
			}
		});
		//显示添加页面-隐藏主页面
		function addUser(){
			$("#add_account").val("");
			$("#add_name").val("");
			$("#add_pwd").val("");
			$("#add_roleID").attr("data-status","0");
			$("#add_roleID").html("选择角色");
			$("#add_EnterpriseId").val("");
			$(".search_div,.table_list").hide();
			$(".turn_page").hide();
			qryRoles(1);
			$("#addUserBox").show();
		}
		//修改页面取消按钮事件
		function edit_cancel(etype){
			$(".search_div,.table_list").show();
			$(".turn_page").show();
			if(etype==1){
				$("#addUserBox").hide();
			}else if(etype==2){
				$("#editUserBox").hide();
			}
		}
		//查询角色
		function qryRoles(type){
			$.ajax({
				url: '../getRoles.do', 
				type: 'POST',
				async: false,
				data:{yema:1,pagelen:10,roleName:""}, 
				dataType: 'json', 
				timeout: 10000, 
				error: function(){LQWarn('请求失败,请稍后重试');}, 
				success: function(result){
					if(result.resultCode==1000){
						var items=result.returnObject;
						if (items.length > 0) {
							var fnav="";
							$.each(items,function(i){
								var item = items[i];
								fnav+='<li><a href="javascript:void(0)" class="choose_a" data-status="'+item.ID+'">'+item.ROLENAME+'</a></li>';
							});
							if(type==2){
								$("#prole_choose_edit").html(fnav);
							}else if(type==1){
								$("#prole_choose_add").html(fnav);
							}
						}
					}else if(result.resultCode==1004){
						LQWarn(result.returnObject);
						top.location.href ="../login.jsp";
					}else{
						LQWarn(result.returnObject);
					}
				}
			}); 
		}
		
		//添加用户
		function doAddUser(){
			var add_account = $.trim($("#add_account").val());
			var add_name = $.trim($("#add_name").val());
			var add_pwd = $("#add_pwd").val();
			var add_roleID=$("#add_roleID").attr("data-status");
			var add_EnterpriseId = $.trim($("#add_EnterpriseId").val());
			if(add_roleID<1){
				LQWarn("请选择角色");
				return;
			}
			if(add_account==""){
			    $("#add_account").focus();
				LQWarn("请输入帐号");
				return;
			}
			if(add_name==""){
			    $("#add_name").focus();
				LQWarn("请输入姓名");
				return;
			}
			if(add_pwd==""){
			    $("#add_name").focus();
				LQWarn("请输入密码");
				return;
			}
			/* if(add_EnterpriseId==""||add_EnterpriseId==null){
			    $("#add_EnterpriseId").focus();
				LQWarn("请输入企业编号");
				return;
			} */
			$.ajax({
				url: 'doAddUser.do', 
				type: 'POST',
				async: false,
				data:{account:add_account,name:add_name,pwd:$.md5(add_pwd),roleId:add_roleID}, 
				dataType: 'json', 
				timeout: 10000, 
				error: function(){LQWarn("请求失败,请稍后重试");}, 
				success: function(result){
					if(result.resultCode==1000){
						LQMsg("添加成功");
						edit_cancel(1);
						qryUserList();
					}else if(result.resultCode==1004){
						LQWarn(result.returnObject);
						top.location.href ="../login.jsp";
					}else{
						LQWarn(result.returnObject);
					}
				}
			}); 
		}
		//编辑提交按钮
		function editUser(ob){
			qryRoles(2);
			var _this=$(ob),
			tr=_this.parents("tr");
			$("#edit_userID").val(tr.attr("data-uid"));
			$("#edit_roleID").attr("data-status",tr.find("input[name=roleID]").val());
			var statusText=tr.find('td').eq(2).text();
			$("#edit_roleID").html(statusText);
			if(statusText=="正常"){
			//tr.find("input[name=status]").val()
			 $("#edit_status").attr("data-status",1);
			}else if(statusText=="禁用"){
			 $("#edit_status").attr("data-status",0);
			}
			 $("#edit_status").attr("data-status",tr.find('td').eq(3).attr("data-status"));
			$("#edit_status").html(tr.find('td').eq(3).text());
			$("#edit_account").val(tr.find('td').eq(0).text());
			$("#edit_name").val(tr.find('td').eq(1).text());
			$(".search_div,.table_list").hide();
			$(".turn_page").hide();
			$("#editUserBox").show();
		}
		//编辑提交按钮
		function doEditUser(){
			var userID=$("#edit_userID").val();
			var edit_account = $.trim($("#edit_account").val());
			var edit_name = $.trim($("#edit_name").val());
			var edit_roleID=$("#edit_roleID").attr("data-status");
			var status = $("#edit_status").attr("data-status");
			if(edit_roleID<1){
				LQWarn("请选择角色");
				return;
			}
			if(edit_account==""){
			    $("#edit_account").focus();
				LQWarn("请输入帐号");
				return;
			}
			if(edit_name==""){
			    $("#edit_name").focus();
				LQWarn("请输入姓名");
				return;
			}
			
			$.ajax({
				url: 'doEditUser.do', 
				type: 'POST',
				data:{id:userID,account:edit_account,name:edit_name,roleId:edit_roleID,status:status},
				async: false,
				dataType: 'json', 
				timeout: 10000, 
				error: function(){LQWarn('请求失败,请稍后重试');}, 
				success: function(result){
					if(result.resultCode==1000){
						LQMsg("修改成功");
						edit_cancel(2);
						qryUserList();
					}
					else if(result.resultCode==1004){
						LQWarn(result.returnObject);
						window.top.location.href ="../login.jsp";
					}else{
						LQWarn(result.returnObject);
					}
				}
			});	
		}
		
		//删除用户
		function delUser(user_id){
			 LQConfirm("您确定要删除该用户吗？",function(){
				$.ajax({
					url: 'delUser.do', 
					type: 'POST',
					data:{userId:user_id}, 
					async: false,
					dataType: 'json', 
					timeout: 10000, 
					error: function(){LQWarn('请求失败,请稍后重试');}, 
					success: function(result){
						if(result.resultCode==1000){
							LQMsg("删除成功");
							qryUserList();
						}
						else if(result.resultCode==1004){
							LQWarn(result.returnObject);
							window.top.location.href ="../login.jsp";
						}else{
							LQWarn(result.returnObject);
						}
					}
				});	
			});
		}
		</script>
	</body>
</html>
